package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.common.util.RequestHolder;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.IsmAppSettingService;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmServiceDefService;
import com.sec.etech.ism.util.IsmStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IsmSendAmendFileServiceImpl implements IsmMethodService {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmServiceDefService ismServiceDefService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject busObj) throws Exception {
        JSONObject result = new JSONObject();
        String msg = this.sendAmendFile(sysCode, serviceCode, busObj);
        result.put("flag", msg);
        result.put("msg", msg);
        return result;
    }

    private String sendAmendFile(String sysCode, String serviceCode, JSONObject busObj){
        Map<String, String> logMap = new HashMap<>(5);
        //服务信息
        IsmServiceDef serviceDef = null;
        try{
            serviceDef = ismServiceDefService.getByCode(serviceCode);
            //调用的应用信息
            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
            String url = appSetting.getAppAddress() + "modificationList/sync/modification";
            logMap.put("appName", this.methodName());
            logMap.put("url", url);
            JSONObject params = new JSONObject();
            String sql = "select t1.amendid,t1.amendnum,t1.fileid,t1.changeTip,t1.templateid," +
                    " t2.plantasknum,t2.plantaskname,t3.component_SAPnum,t4.name templateName " +
                    "from proj_amend t1 join proj_plantask t2 on t1.business_id=t2.plantaskid " +
                    "left join proj_plantask_template t4 on t1.templateid=t4.id " +
                    "left join prod_component t3 on t1.business_id=t3.componentid where t1.amendid=?";
            List<Map<String, Object>> amendList = jdbcTemplate.queryForList(sql, busObj.getLong("amendid"));
            if(amendList!=null && amendList.size()>0) {
                List<JSONObject> modifyList = new ArrayList<>();
                JSONObject object = new JSONObject();
                Object fileId = amendList.get(0).get("fileid");
                object.put("modifyNum", amendList.get(0).get("amendnum"));
                if (fileId != null && (Long) fileId > 0) {
                    object.put("modifyUrl", RequestHolder.getUrl() + "/sys/sysFile/preview.html?audience=drawing&fileId=" + fileId.toString());
                } else {
                    object.put("modifyUrl", "");
                }
                object.put("modifyCause", amendList.get(0).get("changeTip"));
                object.put("planTaskNum", amendList.get(0).get("plantasknum"));
                object.put("planTaskName", amendList.get(0).get("plantaskname"));
                object.put("componentSAPnum", IsmStringUtil.getStrNullToEmpty(amendList.get(0).get("component_SAPnum")));
                object.put("templateName", amendList.get(0).get("templateName"));
                if ("54".equals(amendList.get(0).get("templateid").toString())) {//X2_1流程
                    object.put("isOnlyChangDrawing", true);
                } else {
                    object.put("isOnlyChangDrawing", false);
                }
                modifyList.add(object);
                params.put("modifications", modifyList);
                ismCommonService.checkResult(url, params.toJSONString(), serviceDef, null, busObj.toJSONString(), this.methodName(), logMap);
            }
            return "success";
        }catch (Exception e){
            log.error("修改单文件推送PCS接口异常。serviceDef.getCode()={}，serviceDef.getName()={}，logMap={}",
                    serviceDef.getCode(), serviceDef.getName(), logMap);
            e.printStackTrace();
            return "修改单文件推送PCS接口异常，请联系管理员处理！";
        }
    }

    @Override
    public String methodName() {
        return "sendAmendFile";
    }
}
